/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.rultax.blackice.physics.panel;

import com.rultax.blackice.physics.CoordConverter;
import com.rultax.blackice.physics.Physics;
import java.awt.geom.Point2D;
import javax.swing.JPanel;
import org.jbox2d.common.Vec2;

/**
 *
 * @author scottw
 */
public class PanelCoordConverter implements CoordConverter<Vec2, Point2D.Float> {
    private float transX;
    private float transY;
    private float scaleFactor;
    private float yFlip;
    private JPanel screen;
    
    public PanelCoordConverter(JPanel screen, float transX, float transY, float scaleFactor) {
	this.screen = screen;
	this.transX = transX;
	this.transY = transY;
	this.scaleFactor = scaleFactor;
	yFlip = -1;
    }

    public PanelCoordConverter(float transX, float transY, float scaleFactor, float yFlip) {
	this.transX = transX;
	this.transY = transY;
	this.scaleFactor = scaleFactor;
	this.yFlip = yFlip;
    }
    
    @Override
    public Vec2 screenToWorld(Point2D.Float coord) {
	float worldX = Physics.map(coord.x, transX, transX + scaleFactor, 0, 1f);
	float worldY = coord.y;
	if(yFlip == -1)
	    worldY = Physics.map(coord.y, screen.getHeight(), 0f, 0f, screen.getHeight());
	worldY = Physics.map(worldY, transY, transY + scaleFactor, 0f, 1f);
	return new Vec2(worldX, worldY);
    }

    @Override
    public Point2D.Float worldToScreen(Vec2 coord) {
	float screenX = Physics.map(coord.x, 0f, 1f, transX, transX + scaleFactor);
	float screenY = Physics.map(coord.y, 0f, 1f, transY, transY + scaleFactor);
	if(yFlip == -1)
	    screenY = Physics.map(screenY, 0f, screen.getHeight(), screen.getHeight(), 0f);
	return new Point2D.Float(screenX, screenY);
    }
    
}
